﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CS544_Protocol_Client.Forms
{

    /**
     * Author: Kelly Page
     * Date Created: 2/22/2011
     * Date Updated: 2/24/2011
     * 
     * Purpose: Displays the register user form.
     * 
     **/

    public partial class RegisterForm : Form
    {
        
        private ClientControl myClientControl;
        
        public RegisterForm(ClientControl clientControl)
        {
            InitializeComponent();
            myClientControl = new ClientControl();
            myClientControl = clientControl;
            //myClientControl.RegisterResult += new ClientEventHandler(myClientControl_RegisterResult);
            
        }

        void myClientControl_RegisterResult(object sender, ClientEventArgs e)
        {
            //the MessageBox.Show calls below should be replaced with something more useful,
            //especially in the case of success.

            //if there was an error, we should display the error.
            if (e.Error != null)
                MessageBox.Show(e.Error.ErrorMessage);
            else
                MessageBox.Show("Registration Successful!");
        }


        
        //Handle click event for the register button
        private void RegisterButton_Click(object sender, EventArgs e)
        {
                                  
            //If username textbox is empty then display an error
            //else clear the username error text
            if (this.UserNameTextBox.Text == "")
            {

                this.setUsernameError("Please enter a username.");
                
            }
            else if (this.UserNameTextBox.Text != "")
            {

                this.clearUsernameError();
            }
           

            //if password textbox is empty then display error.
            //else clear the password error text
            if (this.PasswordTextBox.Text == "")
            {

                this.setPasswordError("Please enter a password");
                           
            }
            else if (this.PasswordTextBox.Text != "")
            {

                this.clearPasswordError();

            }



            //if username and password text boxes are filled out then send registration info
            if (this.UserNameTextBox.Text != "" && this.PasswordTextBox.Text != "")
            {

                try
                {
                    //there should be no need for a form handler... the code behind file (this file) IS the form handler :)
                    myClientControl.register(this.UserNameTextBox.Text, this.PasswordTextBox.Text);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

            }
            
        }




        //Sets the username textbox error text
        private void setUsernameError(string error)
        {

            this.UsernameErrorLabel.Text = error;
            this.UsernameErrorLabel.Visible = true;
      
        }//end setUsernameError()



        //Clears username error
        private void clearUsernameError()
        {

            this.UsernameErrorLabel.Text = "";
            this.UsernameErrorLabel.Visible = false;

        }//end clearUsernameError()



        //Sets the password textbox error text
        private void setPasswordError(string error)
        {

            this.PasswordErrorLabel.Text = error;
            this.PasswordErrorLabel.Visible = true;

        }//end setPasswordError



        //Clears the password error
        private void clearPasswordError()
        {

            this.PasswordErrorLabel.Text = "";
            this.PasswordErrorLabel.Visible = false;

        }//end clearPasswordError()




    }
}
